package com.zzzyh.bookbackgroundsystem.controller;


import com.zzzyh.bookbackgroundsystem.constant.Constants;
import com.zzzyh.bookbackgroundsystem.enums.BookStatusEnum;
import com.zzzyh.bookbackgroundsystem.pojo.Book;
import com.zzzyh.bookbackgroundsystem.pojo.Page;
import com.zzzyh.bookbackgroundsystem.pojo.PageData;
import com.zzzyh.bookbackgroundsystem.pojo.Response;
import com.zzzyh.bookbackgroundsystem.service.BooksService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/books")
@Slf4j
public class BooksController {

    @Autowired()  // 引入 BooksService 实例
    private BooksService booksService;
    @RequestMapping("/getBooks")
    public Response getBooks(){
        List<Book> books = booksService.getBooks();
        for(Book book : books){
            book.setStatucCN(BookStatusEnum.getBookStatusEnum(book.getStatus()).getMessage());
        }
        return Response.success(books);
    }

    @RequestMapping("/getBooksByPage")
    public Response getBooksByPage(Page page){
        if(page == null || page.getCurrentPage() == null || page.getPageSize() == null)
            return Response.error("参数错误");
        PageData<Book> bookPageData = new PageData<>();
        bookPageData.setTotal(booksService.getBooksCount());
        List<Book> booksByPage = booksService.getBooksByPage((page.getCurrentPage() - 1) * page.getPageSize(), page.getPageSize());
        for(Book book : booksByPage){
            book.setStatucCN(BookStatusEnum.getBookStatusEnum(book.getStatus()).getMessage());
        }
        bookPageData.setData(booksByPage);
        log.info("bookPageData:{}", bookPageData);
        return Response.success(bookPageData);
    }

    @RequestMapping(value = "/addBook",produces = "application/json;charset=UTF-8")
    public String addBook(@RequestBody Book book){
        if(book == null || book.getBookName() == null || book.getAuthor() == null || book.getCount() == null || book.getPrice() == null || book.getPublish() == null || book.getStatus() == null)
            return "参数错误";
        try {
            if(!booksService.addBook(book))
                return "添加失败";
        }catch (Exception e){
            return e.getMessage();
        }
        return "";
    }

    @RequestMapping("/findBookById")
    public Book findBookById(@RequestParam("bookId") Integer id){
        log.info("id:{}", id);
        if(id == null)
            return null;
        Book book = booksService.findBookById(id);
        book.setStatucCN(BookStatusEnum.getBookStatusEnum(book.getStatus()).getMessage());
        return book;
    }

    @RequestMapping(value = "/updateBook",produces = "application/json;charset=UTF-8")
    public String updateBook(@RequestBody Book book){
        log.info("book:{}", book);
        if(book == null || book.getId() == null || book.getBookName() == null || book.getAuthor() == null || book.getCount() == null || book.getPrice() == null || book.getPublish() == null || book.getStatus() == null)
            return "参数错误";
        try {
            if(!booksService.updateBook(book))
                return "更新失败";
        }catch (Exception e){
            return e.getMessage();
        }
        return "";
    }

    @RequestMapping("/deleteBook")
    public String deleteBook(Integer id){
        if(id == null)
            return "参数错误";
        try {
            if(!booksService.deleteBook(id))
                return "删除失败";
        }catch (Exception e){
            return e.getMessage();
        }
        return "";
    }

    @RequestMapping("/batchDeleteBook")
    public String batchDeleteBook(@RequestParam("ids") List<Integer> ids){
        log.info("ids:{}", ids);
        if(ids == null || ids.size() == 0)
            return "参数错误";
        try {
            for(Integer id : ids){
                if(!booksService.deleteBook(id))
                    return "删除失败";
            }
        }catch (Exception e){
            return e.getMessage();
        }
        return "";
    }

//    @RequestMapping("/batchDeleteBook")
//    public String batchDeleteBook(Integer[] ids){
//        log.info("ids:{}", ids);
//        if(ids == null )
//            return "参数错误";
//        try {
//            for(Integer id : ids){
//                if(!booksService.deleteBook(id))
//                    return "删除失败";
//            }
//        }catch (Exception e){
//            return e.getMessage();
//        }
//        return "";
//    }

}
